package com.higold.cloud.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.higold.cloud.R;
import com.higold.cloud.glide.GlideApp;
import com.higold.cloud.utils.ImageLoader;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.FullScreenPopupView;

import me.zhouzhuo.zzimagebox.ZzImageBox;


/**
 * Description:
 * Create by lxz, at 2019/3/12
 */
public class CustomFullScreenPopup extends FullScreenPopupView {

    private FullScreenPopupClickListener listener;

    public CustomFullScreenPopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layout_publish_sheet;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        findViewById(R.id.tv_sure).setOnClickListener(v -> {
            if (listener != null) {
                listener.okClick();
            }
        });

        findViewById(R.id.tv_cancel).setOnClickListener(v -> {
            if (listener != null) {
                listener.cancelClick();
            }
        });

        ZzImageBox imageBoxAddMode = this.findViewById(R.id.zb_publish);

        //如果加载网络图片，需要设置此代理
        imageBoxAddMode.setOnlineImageLoader((iv, url) -> {
            Log.e("TTT", "url=" + url);
            //使用Glide加载
            GlideApp.with(getContext())
                    .load(url)
                    .placeholder(R.color.color_darker_gray)
                    .transition(new DrawableTransitionOptions().crossFade())
                    .thumbnail(0.5f)
                    .into(iv);
        });

        imageBoxAddMode.setOnImageClickListener(new ZzImageBox.OnImageClickListener() {
            @Override
            public void onImageClick(int position, String filePath, String tag, int type, ImageView iv) {
                new XPopup.Builder(getContext())
                        .asImageViewer(iv, filePath, true, -1, -1, 50, false, new ImageLoader())
                        .show();
            }

            @Override
            public void onDeleteClick(int position, String filePath, String tag, int type) {
                //移除position位置的图片
                imageBoxAddMode.removeImage(position);
            }

            @Override
            public void onAddClick() {
                if (listener != null) {
                    listener.OnImageClick(imageBoxAddMode);
                }
                //添加网络图片
//                imageBoxAddMode.addImageOnlineWithRealPathAndType("http://p1.so.qhimg.com/dmfd/290_339_/t01e15e0f1015e44e41.jpg", "tag" + imageBoxAddMode.getCount(), imageBoxAddMode.getCount());
            }
        });
    }

    public interface FullScreenPopupClickListener {
        public void okClick();

        public void cancelClick();
        public void OnImageClick(ZzImageBox imageBoxAddMode);

    }

    public void setOnFullScreenPopupClickListener(FullScreenPopupClickListener listener) {
        this.listener = listener;
    }
}
